package muta.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import muta.bean.JavaBean;
import muta.bean.SqlBean;

public class SearchById extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * The doDelete method of the servlet. <br>
	 * 
	 * This method is called when a HTTP delete request is received.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		// 获取用户ID
		String sid = request.getParameter("id");
		int id = Integer.parseInt(sid);

		String sql = "select * from student where id=?";
		SqlBean sBean = new SqlBean();
		JavaBean jBean = sBean.getSearchById(sql, id);

		// 用户ID
		request.setAttribute("id", jBean.getId());
		// 用户姓名
		request.setAttribute("name", jBean.getName());
		// 用户密码
		request.setAttribute("password", jBean.getPassword());

		// 用户性别
		String sex = "";
		String man = "";
		String woman = "";
		if (jBean.getSex() != null) {
			sex = jBean.getSex().trim();
			if (sex.equals("男")) {
				man = "checked";
			} else {
				woman = "checked";
			}
		}
		request.setAttribute("man", man);
		request.setAttribute("woman", woman);

		// 用户年龄
		request.setAttribute("age", jBean.getAge());

		// 转发
		request.getRequestDispatcher("Update.jsp").forward(request, response);
	}
}